## 

rm(list = ls())

library(tidyverse)
library(fixest)
library(knitr)
library(kableExtra)
library(DIDmultiplegt)

## Get helper functions

source("code/helper_functions.R")

## Get estimates

res <- read_rds("data/Results_Main.rds") %>%
  mutate(effect_sd = estimate / dv_sd)

## Prep for table

res_tab <- res %>%
  filter(str_detect(outcome, "polar_bund_diff|small_party")) %>%
  filter(str_detect(ss, "Combined|Pre|Post|NoEastIn94")) %>%
  filter(!(ss == "NoEastIn94" & unit == "Municipal election")) %>%
  filter(!(str_detect(ss, "2000") & unit == "Municipal election")) %>%
  filter(!str_detect(outcome, "small_party2")) %>%
  filter(period_numeric == 1) %>%
  filter(str_detect(term, "Decrease")) %>%
  mutate(outcome = dplyr::recode(outcome,
    `small_party_diff` = "Small party vote share",
    `polar_bund_diff` = "Polarization"
  )) %>%
  filter(!unit == "State election") %>%
  arrange(ss) %>%
  dplyr::select(
    -period, -term, -conf.low, -conf.high,
    -east, -period_numeric,
    -conf.low90, -conf.high90,
    -main_spec, -effect_sd
  ) %>%
  mutate(
    fe = as.character(fe),
    unit = as.character(unit)
  ) %>%
  dplyr::select(
    ss, outcome, unit, fe, estimate,
    std.error, statistic, p.value, n
  ) %>%
  mutate(ss = dplyr::recode(ss,
    `NoEastIn94` = "4. Full sample (omitting East Germany in 1994)",
    `Combined` = "1. Full sample",
    `Post 2005` = "3. After 2000",
    `Pre 2005` = "2. Before 2000"
  )) %>%
  arrange(ss) %>%
  mutate(unit = str_remove(unit, " election"))

## Make table 
## Groupings 

gr <- res_tab %>% ungroup() %>%
  mutate(rowid = 1:n()) %>%
  group_by(ss) %>%
  summarise(first = first(rowid),
            last = dplyr::last(rowid)) %>%
  data.frame(stringsAsFactors = F)

## Format the n column such that it has , for thousands
## Using format 

res_tab$n <- format(res_tab$n, big.mark = ",")


# Table A.4

kable(res_tab %>% dplyr::select(-ss), 
      "latex", 
      longtable = F, 
      booktabs = T, col.names = c('Outcome',
                                  'Election',
                                  'Model',
                                  'Estimate',
                                  'SE', 
                                  'T-stat',
                                  'P',
                                  'N'),
      linesep = "", 
      caption = 'Main results',
      label = 'tab:main_appendix',
      escape = F, digits = 3) %>%
  kable_styling(latex_options = c("repeat_header"),
                font_size = 10) %>% 
  # collapse_rows() %>% 
  row_spec(0, bold = T) %>% 
  column_spec(1, width = '4.5cm') %>% 
  kable_styling(latex_options = "HOLD_position") %>% 
  pack_rows(gr[1, 'ss'], gr[1, 'first'], gr[1, 'last'],
            hline_after = F) %>%
  pack_rows(gr[2, 'ss'], gr[2, 'first'], gr[2, 'last'],
            hline_after = F) %>%
  pack_rows(gr[3, 'ss'], gr[3, 'first'], gr[3, 'last'],
            hline_after = F)%>%
  pack_rows(gr[4, 'ss'], gr[4, 'first'], gr[4, 'last'],
            hline_after = F)

## Load main data

btw <- readRDS("data/data_main.RDS") %>%
  mutate(treat_categorical4 = factor(treat_categorical4,
    levels = c(
      "NoChange",
      "DecAndInc",
      "Increase",
      "Decrease"
    )
  )) %>%
  filter(!year == 2013) %>%
  mutate(y_new = small_party_zweit_diff - left_zweit_diff)

## Municipal data

muni <- readRDS("data/data_main_municipal.RDS") %>%
  mutate(treat_categorical4 = factor(treat_categorical4,
    levels = c(
      "NoChange",
      "DecAndInc",
      "Increase",
      "Decrease"
    )
  ))

## Year times East specification

m1 <- feols(polar_bund_alt_diff ~ treat_categorical4 | year ,
            data = btw %>% filter(year < 2000),
            cluster = ~ county_id_2018)
m2 <- feols(small_party_zweit_diff ~ treat_categorical4 | year,
            data = btw %>% filter(year < 2000),
            cluster = ~ county_id_2018)
m3 <- feols(polar_bund_alt_diff ~ treat_categorical4 |   year^east,
            data = btw %>% filter(year < 2000),
            cluster = ~ county_id_2018)
m4 <- feols(small_party_zweit_diff ~ treat_categorical4 | year^east,
  data = btw %>% filter(year < 2000),
  cluster = ~county_id_2018
)

## Dictionary

setFixest_dict(c(treat_categorical4Decrease = 'Newspaper exit',
                 polar_bund_alt_diff = 'Polarization',
                 small_party_zweit_diff = 'Small party vote share (0-1)',
                 small_party_diff = 'Small party vote share (0-1)',
                 year = 'Year',
                 east = 'East Germany',
                 county_id_2018 = 'County-level'))

# DC & H

##  Treatment defined as decrease (binary)

btw <- btw %>% 
  mutate(treat_decrease = 1*(treat_categorical4 == 'Decrease'))

## Def function that tidies the output

tidy.did_multiplegt = function(x, level = 0.95) {
  ests = x[grepl("^placebo_|^effect|^dynamic_", names(x))]

  ret = data.frame(
    term      = names(ests),
    estimate  = as.numeric(ests),
    std.error = as.numeric(x[grepl("^se_placebo|^se_effect|^se_dynamic", names(x))]),
    N         = as.numeric(x[grepl("^N_placebo|^N_effect|^N_dynamic", names(x))])
  ) %>%
    # For CIs we'll assume standard normal distribution
    mutate(
      conf.low = estimate - std.error * (qnorm(1 - (1 - level) / 2)),
      conf.high = estimate + std.error * (qnorm(1 - (1 - level) / 2)),
      t.stat = estimate / std.error,
      p.value = 2 * (1 - pnorm(abs(t.stat)))
    )

  return(ret)
}

## Overall

set.seed(123)

out <- did_multiplegt(
  cluster = "county_id_2018",
  brep = 2,
  df = btw %>%
    filter(year < 2001),
  Y = "polar_bund_alt",
  G = "county_id_2018",
  T = "year",
  D = "treat_decrease"
)

dh1 <- tidy.did_multiplegt(out) %>%
  dplyr::rename(n = N)

## Small party outcome

set.seed(123)

out_sp <- did_multiplegt(
  cluster = "county_id_2018",
  brep = 2,
  df = btw %>%
    filter(year < 2001),
  Y = "small_party_zweit",
  G = "county_id_2018",
  T = "year",
  D = "treat_decrease"
)

## Tidy

dh2 <- tidy.did_multiplegt(out_sp) %>%
  dplyr::rename(n = N)

## Get results

out_table <- bind_rows(m1 %>% tidy_feols(),
                       m3 %>% tidy_feols(),
                       dh1,
                       m2 %>% tidy_feols(),
                       m4 %>% tidy_feols(),
                       dh2) %>% 
  filter(str_detect(term, "effect|Decrease")) %>% 
  dplyr::select(estimate, std.error, p.value, n, rsq) %>% 
  mutate(outcome = rep(c("polar", "small_party"), each =3)) %>% 
  mutate(fe = rep(c("year", "year*east", "chaisemartin"), 2))

## To show this via etable, we need to create dummy models
## Then, replace the dummy coefs with the DC&H estimates

dummy_m1 <- m1
dummy_m1$coefficients["treat_categorical4Decrease"] <-  dh1$estimate
dummy_m1$se["treat_categorical4Decrease"] <- dh1$std.error
dummy_m1$nobs <- dh1$N

dummy_m2 <- m2
dummy_m2$coefficients["treat_categorical4Decrease"] <-  dh2$estimate
dummy_m2$se["treat_categorical4Decrease"] <- dh2$std.error
dummy_m2$nobs <- dh2$N

## Prep for table

mlist <- list(m1, m3, dummy_m1, m2, m4, dummy_m2)

## Table A.5

etable(mlist, tex = TRUE, cluster = 'county_id_2018', 
       keep = 'exit',
       digits = 3, 
       digits.stats = 3,
       fixef_sizes=  F,
       style.tex = style.tex("qje",
                             yesNo = c("Yes", "No"),
                             fixef.where = 'var',
                             fixef.suffix = ' FEs',
                             tablefoot = T,
                             tablefoot.value = 'default'),
       fitstat = ~ r2 + n,
       title = 'Results allowing for differential trends between East and West Germany')

## Split-ticket voting

spl <- read_rds("data/elections_split_ticket.rds") %>%
  mutate(year = as.numeric(year))

## Merge to main data

btw <- btw %>% 
  left_join(spl) %>% 
  mutate(across(matches("split_ticket_abs"), ~.*100))

## Define outcome vars

dvars <- btw %>%
  colnames() %>%
  str_filter("split_ticket_abs") %>%
  str_filter("afd", neg = T)

dvars %>% dput()

dvars_dict <- data.frame(
  dv = c(
    "split_ticket_abs",
    "split_ticket_abs_cdu_csu",
    "split_ticket_abs_fdp",
    "split_ticket_abs_greens",
    "split_ticket_abs_left",
    "split_ticket_abs_spd"
  ),
  lab = c(
    "Overall", "CDU/CSU",
    "FDP", "Greens", "Left party",
    "SPD"
  )
)

## Dictionary

fe_dict <- c(dvars_dict[, 2])

names(fe_dict) = dvars_dict[, 1]

setFixest_dict(fe_dict)

m1 <- feols(.[dvars] ~ treat_categorical4 | year,
  data = btw,
  cluster = ~county_id_2018
)

# Table A.10

etable(m1, tex = TRUE, cluster = 'county_id_2018', 
       keep = 'exit',
       digits = 3, 
       digits.stats = 3,
       fixef_sizes=  F,
       style.tex = style.tex("qje",
                             yesNo = c("Yes", "No"),
                             fixef.where = 'var',
                             fixef.suffix = ' FEs',
                             tablefoot = T,
                             tablefoot.value = 'default'),
       fitstat = ~ r2 + n,
       title = 'Split-ticket voting')

## Standardized outcome ##

m1 <- feols(polar_bund_alt_std_diff ~ treat_categorical4 | year,
  data = btw,
  cluster = ~county_id_2018
)

m2 <- feols(polar_bund_alt_std_diff ~ treat_categorical4 | year,
  data = btw %>% filter(year < 2000),
  cluster = ~county_id_2018
)

m3 <- feols(polar_bund_alt_std_01 ~ treat_categorical4 | year,
  data = btw,
  cluster = ~county_id_2018
)

m4 <- feols(polar_bund_alt_std_01 ~ treat_categorical4 | year,
  data = btw %>% filter(year < 2000),
  cluster = ~county_id_2018
)

## Table A.8

etable(list(m1, m2, m3, m4),
  tex = TRUE, cluster = "county_id_2018",
  keep = "exit",
  digits = 3,
  digits.stats = 3,
  fixef_sizes = F,
  style.tex = style.tex("qje",
    yesNo = c("Yes", "No"),
    fixef.where = "var",
    fixef.suffix = " FEs",
    tablefoot = T,
    tablefoot.value = "default"
  ),
  fitstat = ~ r2 + n
)


## By party

## Party list

pvars <- c(
  "fdp_zweit_diff", "greens_zweit_diff",
  "left_zweit_diff"
)

## Model

m1_parties <- feols(.[pvars] ~ treat_categorical4 | year,
  data = btw,
  cluster = ~county_id_2018
)

## Table A.7 

etable(m1_parties, tex = TRUE, cluster = 'county_id_2018',
       keep = 'exit',
       digits = 3,
       digits.stats = 3,
       fixef_sizes=  F,
       style.tex = style.tex("qje",
                             yesNo = c("Yes", "No"),
                             fixef.where = 'var',
                             fixef.suffix = ' FEs',
                             tablefoot = T,
                             tablefoot.value = 'default'),
       fitstat = ~ r2 + n)






